package bd403.namespace;

import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import bd403.Backend.DrinkStorage;
import bd403.Backend.Help;
import bd403.UIComponents.AddActiv;
import bd403.UIComponents.HelpActiv;
import bd403.UIComponents.HomeActiv;
import bd403.UIComponents.SearchActiv;

/* UI container for the BrewDawg app. Setup of tab host and instantiation
 * of helper activities.
 * Main author: Mike
 */

public class BDActivity extends TabActivity {
        
    private static DrinkStorage storage; // DrinkStorage Application class for this app
    private static Help help;
	
	/* Called when this Activity is created. Sets up the XML layout and Tab view
	 * for the application.
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.main);
		storage = (DrinkStorage)getApplicationContext();
		help = new Help(this.getApplicationContext());
		initializeTabs();
	}
	
	/* Accessor to get a reference to DrinkStorage between activities
     * in different packages.
     */
    public static DrinkStorage getStorage() {
            return storage;
    }
    
    /* Returns an instance of the help activity for this application.
     */
    public static Help getHelp() {
            return help;
    }
	
    /* Sets up the options menu for the container.
     */
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(getString(R.string.title_help));
		return true;
    }
	
	/* Launches the Help page once it is clicked from the options menu.
	 */
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		Intent i = new Intent(this, HelpActiv.class);
        startActivity(i);
        return true;
    }
	
	/* Called when the application is started. Sets up a TabHost to contain Activity
	 * classes for the UI.
	 */
	private void initializeTabs() {
		createTab(R.drawable.tab, getString(R.string.title_home), HomeActiv.class);
		createTab(R.drawable.tab, getString(R.string.title_add), AddActiv.class);
		createTab(R.drawable.tab, getString(R.string.title_search), SearchActiv.class);
	}
	
	/* Helper function to add an Activity class to the TabHost for the UI.
	 */
	private void createTab(int drawableId, String labelId, Class activity) {
		TabHost tabHost = getTabHost();
		Intent intent = new Intent(this, activity);
		TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);	
		
		View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tindicator, getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(labelId);
 
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);
		
		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
	}
}